#!/bin/sh

. ../eval_tools.sh

HEADER 'snmptrapd embedded perl support (NetSNMP::TrapReceiver)'

SKIPIF DISABLE_SNMPV2C
SKIPIFNOT NETSNMP_EMBEDDED_PERL

#
# Begin test
#

snmp_version=v2c
TESTCOMMUNITY=testcommunity
TESTOID=.1.3.6.1.6.3.1.1.5.1
EMBPERL_LOGFILE=${SNMP_TMPDIR}/embperl.log

echo "use blib qw(${SNMP_UPDIR}/perl);" > ${SNMP_TMPDIR}/snmp_perl_trapd.pl
echo "use NetSNMP::TrapReceiver;" >> ${SNMP_TMPDIR}/snmp_perl_trapd.pl

CONFIGTRAPD authcommunity execute $TESTCOMMUNITY
#CONFIGTRAPD doNotLogTraps true
CONFIGTRAPD perlInitFile ${SNMP_TMPDIR}/snmp_perl_trapd.pl
CONFIGTRAPD 'perl sub my_receiver { open LOG,">'$EMBPERL_LOGFILE'"; print LOG "handled_by_embperl"; close LOG };'
CONFIGTRAPD 'perl NetSNMP::TrapReceiver::register("'$TESTOID'", \&my_receiver) || warn "registered_perl_handler_failed\n";'
CONFIGTRAPD 'perl print STDERR "registered_perl_handler_ok\n";'

STARTTRAPD

CAPTURE "snmptrap -d -Ci -$snmp_version -c $TESTCOMMUNITY $SNMP_TRANSPORT_SPEC:$SNMP_TEST_DEST$SNMP_SNMPTRAPD_PORT 0 $TESTOID system.sysContact.0 s handled_trap_$snmp_version"

STOPTRAPD

CHECKFILE $EMBPERL_LOGFILE "handled_by_embperl"
#CHECKTRAPD "handled_by_embperl"

FINISHED
